using System;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using MediaPortal.GUI.Video;
//using MediaPortal.Video.Database;
using MediaPortal.Util;
using MediaPortal.Dialogs;
using MediaPortal.Player;
using System.Data;
using System.ComponentModel;
using c3o.Framework.Web;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using AzureusWrapper;
using System.Text.RegularExpressions;


namespace MediaPortal.GUI.Rss
{
	public class GUITorrents : GUIRssBaseWindow, ISetupForm
	{
		[SkinControlAttribute(100)]		protected GUIButtonControl bttnAddSearchTag = null;
		[SkinControlAttribute(310)]		protected GUIButtonControl bttnuTorrent = null;
        [SkinControlAttribute(320)]     protected GUIButtonControl bttnAzureus = null;

        bool ShowuTorrentButton = true;
        bool ShowAzureusButton = true;

		protected int pageCount = 0;
		protected string SearchString = null;
		protected UKNovaReader dtUKNova;
		private AzureusHost azureusHost;

		//Download[] downloads;
		
		//private System.Windows.Forms.Timer _AzureusTimer = new System.Windows.Forms.Timer();

		public override int GetID
		{
			get
			{
				return 61901;
			}
			set
			{
			}
		}

        protected override void LoadSettings()
        {
            base.LoadSettings();

            this.xmlSection = "torrents";
            
            this.RssFileName = "MyTorrents.Feeds.opml.xml";
            this.OpmlFileName = "MyTorrents.Search.opml.xml";

            if (dtUKNova == null)
            {
                MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings("MediaPortal.xml");

                //this.DefaultSiteView = (GUIFacadeControl.ViewMode)xmlreader.GetValueAsInt("torrents", "Site.ViewMode", (int)GUIFacadeControl.ViewMode.LargeIcons);

                ShowuTorrentButton = xmlreader.GetValueAsBool("plugins", "My uTorrent", true);
                ShowAzureusButton = xmlreader.GetValueAsBool("plugins", "My Azureus", true);

                string Login = xmlreader.GetValue(this.xmlSection, "UKNova.Login") as string;
                string Password = xmlreader.GetValue(this.xmlSection, "UKNova.Password") as string;
                dtUKNova = new UKNovaReader(Login, Password);

                //string azureusFlag = xmlreader.GetValue(this.xmlSection, "Azureus.enabled");

                if (ShowAzureusButton)
                {
                    if (azureusHost == null)
                    {
                        string url = xmlreader.GetValue(this.xmlSection, "Azureus.url");

                        if (url == null || url.Length == 0) url = "http://127.0.0.1:6884/process.cgi";

                        try
                        {
                            azureusHost = new AzureusHost(url);
                        }
                        catch
                        {
                            azureusHost = null;
                            //this.bttnShowDownload.Disabled = true;

                            GUIDialogOK pDlgOK = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
                            pDlgOK.SetHeading("Azureus " + GUILocalizeStrings.Get(257));//Error
                            pDlgOK.SetLine(1, url);
                            pDlgOK.DoModal(GUIWindowManager.ActiveWindow);
                        }
                    }
                }
            }

        }



		protected override void OnPageLoad()
		{
            base.OnPageLoad();

            GUIPropertyManager.SetProperty("#header.label", "My Torrents");

            this.bttnAzureus.Disabled = !this.ShowAzureusButton;
            this.bttnuTorrent.Disabled = !this.ShowuTorrentButton;		
		}



        //private void ShowDownloads(object sender, System.EventArgs e)
        //{
        //    this.ShowDownloads();
        //}

		public override bool Init()
		{
			//this.RssFileName = "MyTorrents.Feeds.opml.xml";
			//this.OpmlFileName = "MyTorrents.Search.opml.xml";

			//g_Player.PlayBackStopped += new MediaPortal.Player.g_Player.StoppedHandler(OnPlayBackStopped);
			//g_Player.PlayBackEnded += new MediaPortal.Player.g_Player.EndedHandler(OnPlayBackEnded);
			//g_Player.PlayBackStarted += new MediaPortal.Player.g_Player.StartedHandler(OnPlayBackStarted);

			return Load(GUIGraphicsContext.Skin + @"\MyTorrents.xml");
		}

		protected override void ShowSites()
		{
            //if (this._AzureusTimer != null)
            //{
            //    if (this._AzureusTimer.Enabled)
            //    {
            //        this._AzureusTimer.Stop();
            //    }
            //}

			this.ListMode = "Sites";

			this.SearchString = null;

			GUIControl.ClearControl(this.GetID, facadeView.GetID);	
			this.facadeView.View = this.DefaultView;

			if (this.dtUKNova != null && this.dtUKNova._Login != null && this.dtUKNova._Login.Length > 0)
			{
				// Download & Cache BBC logo
				this.DownloadAndCache(@"http://www.eguk.org.uk/DMU02/images/bbc_logo1.GIF", "UKNova");
				string coverArtImage = MediaPortal.Util.Utils.GetCoverArtName(Thumbs.MovieTitle, "UKNova");
				string largeCoverArtImage = MediaPortal.Util.Utils.GetLargeCoverArtName(Thumbs.MovieTitle, "UKNova");

				// Add UKNova
				GUIListItem item = new GUIListItem();
				item.Label = "UKNova";
				item.IsFolder = true;
				item.ThumbnailImage = coverArtImage;
				item.IconImage = coverArtImage;
				item.IconImageBig = largeCoverArtImage;
				item.Path = "UKNova";
				facadeView.Add(item);
			}


			base.ShowSites(false);

			this.bttnAddSearchTag.Disabled = true;

			// Clear page count so search lists start at 0
			this.pageCount = 0;

			//if (this.dtUKNova != null && this.dtUKNova._Login != null && this.dtUKNova._Login.Length > 0)
			//{
			//    // Download & Cache BBC logo
			//    this.DownloadAndCache(@"http://www.eguk.org.uk/DMU02/images/bbc_logo1.GIF", "UKNova");
			//    string coverArtImage = MediaPortal.Util.Utils.GetCoverArtName(Thumbs.MovieTitle, "UKNova");
			//    string largeCoverArtImage = MediaPortal.Util.Utils.GetLargeCoverArtName(Thumbs.MovieTitle, "UKNova");

			//    // Add UKNova
			//    GUIListItem item = new GUIListItem();
			//    item.Label = "UKNova";
			//    item.IsFolder = true;
			//    item.ThumbnailImage = coverArtImage;
			//    item.IconImage = coverArtImage;
			//    item.IconImageBig = largeCoverArtImage;
			//    item.Path = "UKNova";
			//    facadeView.Add(item);
			//}
		}


		protected override void ShowSite()
		{
			//this.SearchString = null;

			this.bttnDelete.Disabled = false;
			this.bttnDelete.Label = "Delete Search Tag";

			// Clear page count so search lists start at 0
			this.pageCount = 0;
			this.SearchString = "";
			
			if (this.CurrentSite == "UKNova")
			{
				this.bttnAddSearchTag.Disabled = false;

				this.ShowDetailsAsc();
				//this.bttnAddSearchTag.Disabled = false;
				MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings("MediaPortal.xml");

				// Add Folders
				GUIListItem item = null;

				string SearchTags = xmlreader.GetValue(this.xmlSection, "Site." + this.CurrentSite + ".SearchTags") as string;

				if (SearchTags != null && SearchTags.Length > 0)
				{
					ListMode = "Sites.Tags";

					// Clear List
					GUIControl.ClearControl(this.GetID, facadeView.GetID);
					this.facadeView.View = this.DefaultView;

					item = new GUIListItem();
					item.Label = "..";
					item.IsFolder = true;
					item.IconImage = "defaultVideo.png";
					item.Path = ".";
					facadeView.Add(item);

					item = new GUIListItem();
					item.Label = "All";
					item.IsFolder = true;
					item.IconImage = "defaultVideo.png";
					item.Path = "";
					item.TVTag = this.selectedOutline;
					facadeView.Add(item);

					string[] TagList = SearchTags.Split(',');

					foreach (string Tag in TagList)
					{
						item = new GUIListItem();
						item.Label = Tag;
						item.IsFolder = true;
						item.IconImage = "defaultVideo.png";
						item.Path = Tag;
						item.TVTag = this.selectedOutline;
						facadeView.Add(item);
					}
				}
				else
				{
					this.ShowTorrents();
				}
			}
			else
			{
				base.ShowSite();
			}
		}

		public void ShowDetailsAsc()
		{
			// Load Page in background
			BackgroundWorker worker = new BackgroundWorker();
			worker.DoWork += new DoWorkEventHandler(this.ShowDetails);
			worker.RunWorkerAsync();
		}

		protected void ShowDetails(object sender, DoWorkEventArgs e) { this.ShowDetails(); }
		protected void ShowDetails()
		{
			string list = "";

			MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.Settings("MediaPortal.xml");
			string UserID = xmlreader.GetValue(this.xmlSection, "UKNova.UserID") as string;

			if (UserID != null && UserID.Length > 0)
			{
				StringDictionary sd = dtUKNova.GetProfile(UserID);

				foreach (string key in sd.Keys)
				{
					list += key + ": " + sd[key] + "\n";
				}

				//  GUIPropertyManager.SetProperty("#details", list);
				this.detailbox.Label = list;
			}
		}


		#region ISetupForm Members

		// Returns the name of the plugin which is shown in the plugin menu
		public string PluginName()
		{
			return "My Torrents";
		}



		// Returns the description of the plugin is shown in the plugin menu
		public string Description()
		{
			return "My Torrents";
		}



		// Returns the author of the plugin which is shown in the plugin menu
		public string Author()
		{
			return "fcsobel";
		}



		// show the setup dialog
		public void ShowPlugin()
		{
			Form setup = new MyTorrentsSetupForm();
			setup.ShowDialog();
		}



		// Indicates whether plugin can be enabled/disabled
		public bool CanEnable()
		{
			return true;
		}

		// get ID of windowplugin belonging to this setup
		public int GetWindowId()
		{
			return this.GetID;
		}
	
		// Indicates if plugin is enabled by default;
		public bool DefaultEnabled()
		{
			return true;
		}

		// indicates if a plugin has its own setup screen
		public bool HasSetup()
		{
			return true;
		}


		/// <summary>
		/// If the plugin should have its own button on the main menu of Media Portal then it
		/// should return true to this method, otherwise if it should not be on home
		/// it should return false
		/// </summary>
		/// <param name="strButtonText">text the button should have</param>
		/// <param name="strButtonImage">image for the button, or empty for default</param>
		/// <param name="strButtonImageFocus">image for the button, or empty for default</param>
		/// <param name="strPictureImage">subpicture for the button or empty for none</param>
		/// <returns>true  : plugin needs its own button on home
		///          false : plugin does not need its own button on home</returns>
		//public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
		//{

		//    strButtonText = "UKNova";
		//    strButtonImage = String.Empty;
		//    strButtonImageFocus = String.Empty;
		//    strPictureImage = String.Empty;
		//    return false;
		//}

		public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
		{
			strButtonText = PluginName();
			strButtonImage = String.Empty;
			strButtonImageFocus = String.Empty;
			strPictureImage = String.Empty;
			return true;
		}
		#endregion


		protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
		{
			base.OnClicked(controlId, control, actionType);

			//if (control == this.bttnAddSite) this.ShowOpmlSites();
			//if (control == this.facadeView)     this.OnListView();
			//if (control == this.bttnAddSearchTag) this.AddSearchTag();
			//if (control == this.bttnDelete) this.OnDelete();
			if (control == this.bttnAddSearchTag) this.AddSearchTag();

            //if (control == this.bttnShowDownload)
            //{
            //    if (this.ListMode == "Torrents.Azureus")
            //    {
            //        this._AzureusTimer.Stop();
            //        this.ShowSites();
            //        this.bttnAddSite.Disabled = false;
            //        //this.btnViewAs.Disabled = false;
            //        this.bttnShowDownload.Label = "Azureus";
            //    }
            //    else
            //    {
            //        this.ShowDownloads();
            //        this.bttnAddSite.Disabled = true;
            //        //this.btnViewAs.Disabled = true;
            //        this.bttnShowDownload.Label = "Back";
            //    }
            //}
		}



		public override void OnAction(Action action)
		{
			base.OnAction(action);

			if (action.IsUserAction())
			{
				GUIListItem item = this.facadeView.SelectedListItem;

				if (item != null)
				{
					// Auto Load UKNova if last item on list is not a folder
					if (this.CurrentSite == "UKNova" && this.ListMode == "Sites.List")
					{
						if (!item.IsFolder)
						{
							if (facadeView.SelectedListItemIndex == facadeView.Count - 1)
							{
								this.ShowTorrents();
							}
						}
					}

                    //if (this.ListMode == "Torrents.Azureus")
                    //{
                    //    this.ShowStats();					
                    //}
				}
			}
		}


		protected void ShowStats()
		{
			GUIListItem item = this.facadeView.SelectedListItem;

			if (item != null)
			{
				Download dl = item.TVTag as Download;

				if (dl != null)
				{
					this.detailbox.Label = "Size:" + dl.TorrentInformation.Size + " " +
											"Status:" + dl.DownloadStats.Status + " " +
											"ETA:" + dl.DownloadStats.ETA.ToString() + " " +
											"Ratio:" + dl.DownloadStats.ShareRatio.ToString();
				}
			}
		}


		//public override void OnAction(Action action)
		//{
		//    base.OnAction(action);

		//    if (action.IsUserAction())
		//    {
		//        GUIListItem item = this.facadeView.SelectedListItem;

		//        if (item != null)
		//        {
		//            // Set Description
		//            this.detailbox.Label = item.DVDLabel;

		//            // Auto Load UKNova
		//            if (!item.IsFolder)
		//            {
		//                if (facadeView.SelectedListItemIndex == facadeView.Count - 1)
		//                {
		//                    if (this.ListMode == "Sites.List" && this.CurrentSite == "UKNova") this.ShowTorrents();
		//                }
		//            }
		//        }
		//    }
		//}


		protected override void OnDelete()
		{
			base.OnDelete();

			switch (this.ListMode)
			{
				case "Sites.Tags":
					// Delete Search Tab
					this.DeleteSearchTag(this.CurrentSite, this.SearchString);
					break;
			}
		}


		protected override void OnListViewList()
		{
			base.OnListViewList();

			GUIListItem item = this.facadeView.SelectedListItem;

			if (item != null)
			{
				if (item.IsFolder)
				{
					if (item.Label == "..")
					{
						this.ShowSites();
					}
					else
					{
						this.ShowTorrents();
					}					
				}
				else
				{
					// Select Item
					item.IconImage = "check-box.png";
					item.IsDownloading = true;

					// Refresh list so you see change
					GUIControl.RefreshControl(this.GetID, this.facadeView.GetID);

					// Download File
					string url = this.facadeView.SelectedListItem.Path;

					this.GetTorrent(url);
//					if (url != null && url.Length > 0)
//					{
//						url = url.Replace(@"/tor/", @"/get/");
//						dtUKNova.DownloadTorrent(url);
//					}
				}
			}
		}


		//protected void GetTorrent(string url, string pattern, string replace)
		//{
		//    Regex reg = new Regex(pattern);
		//    url = reg.Replace(JsonData, replace);
		//}


		protected void GetTorrent(string url)
		{
			if (url != null && url.Length > 0)
			{
				// Added for mininova rss
				//url = url.Replace(@"/tor/", @"/get/");

				//Regex reg = new Regex(@".*www\.torrentspy\.com/torrent/(.*)/.*");
				//url = reg.Replace(JsonData, @"http://cache.torrentspy.com/download.asp?id=\1");

				if (this.CurrentSite == "UKNova")
				{
					dtUKNova.DownloadTorrent(url);
				}
				else
				{
					if (azureusHost != null)
					{
						azureusHost.AddDownload(new Uri(url));
					}
					else
					{
						this.dtRss.DownloadTorrent(url);
						//dtUKNova.DownloadTorrent(url);
					}
				}
			}			
		}


		protected override void OnListView()
		{
			string mode = this.ListMode;

			base.OnListView();

			if (this.selectedOutline == null)
			{
				switch (mode)
				{
					case "Sites":
						this.OnListViewSites();
						break;
					case "Sites.Tags":
						this.OnListViewTags();
						break;
                    //case "Torrents.Azureus":
                    //    this.AzureusAction("Toggle");
                    //    break;
                    //case "Torrents.uTorrent":
                    //    this.uTorentAction("Toggle");
                    //    break;
				}
			}
		}




	

		protected void OnListViewTags()
		{
			GUIListItem item = this.facadeView.SelectedListItem;

			if (item != null)
			{
				if (item.IsFolder)
				{
					switch (item.Label)
					{
						case "..":
							this.ListMode = "Sites";
							this.ShowSites();
							break;
						default:
							this.SearchString = item.Path;
							switch (this.CurrentSite)
							{
								case "UKNova":
									// Show All
									this.ShowTorrents();
									break;
								default:
									this.ShowRSS();
									break;
							}
							break;
					}
				}
			}
		}

		protected void AddSearchTag()
		{
			string NewSearchTag = String.Empty;

			if (GetKeyboard(ref NewSearchTag))
			{
				MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.Settings("MediaPortal.xml");
				string SearchTags = xmlwriter.GetValue(this.xmlSection, "Site." + this.CurrentSite + ".SearchTags") as string;

				if (SearchTags != null && SearchTags.Length > 0)
				{
					SearchTags += "," + NewSearchTag;
				}
				else
				{
					SearchTags = NewSearchTag;
				}

				xmlwriter.SetValue(this.xmlSection, "Site." + this.CurrentSite + ".SearchTags", SearchTags);

				this.ShowSite();
			}
		}

		protected void DeleteSearchTag(string Site, string tag)
		{
			MediaPortal.Profile.Settings xmlwriter = new MediaPortal.Profile.Settings("MediaPortal.xml");
			string SearchTags = xmlwriter.GetValue(this.xmlSection, "Site." + Site + ".SearchTags") as string;

			if (SearchTags != null && SearchTags.Length > 0)
			{
				// Remove Search Tag
				SearchTags = "," + SearchTags + ",";
				SearchTags = SearchTags.Replace("," + tag + ",", ",");
				SearchTags = SearchTags.Substring(1, SearchTags.Length - 2);
				xmlwriter.SetValue(this.xmlSection, "Site." + Site + ".SearchTags", SearchTags);
			}
			this.ShowSite();
		}




		//public void DownloadTorrent(string url)
		//{
		//    int i = 1;

		//    if (url != null && url.Length > 0)
		//    {
		//        if (!System.IO.Directory.Exists(this.TorrentPath)) System.IO.Directory.CreateDirectory(this.TorrentPath);

		//        // Find file name
		//        string FileName = this.TorrentPath + @"\temp" + i.ToString() + ".torrent";
		//        while (System.IO.File.Exists(FileName))
		//        {
		//            i++;
		//            FileName = this.TorrentPath + @"\temp" + i.ToString() + ".torrent";
		//        }

		//        // Save Torrent file to disk
		//        this.sr.Download(url, FileName);

		//        // Launch client
		//        System.Diagnostics.Process.Start(FileName);
		//    }
		//}


		public void ShowTorrentsAsc()
		{
			// Load Page in background
			BackgroundWorker worker = new BackgroundWorker();
			worker.DoWork += new DoWorkEventHandler(this.ShowTorrents);
			worker.RunWorkerAsync();
		}
		// Background Version
		public void ShowTorrents(object sender, DoWorkEventArgs e) { this.ShowTorrents(); }
		public void ShowTorrents()
		{
			this.bttnAddSearchTag.Disabled = false;
			this.bttnDelete.Disabled = true;
			this.bttnDelete.Label = "Delete";

			this.ListMode = "Sites.List";

			GUIPropertyManager.SetProperty("#selecteditem", "Loading...");
			GUIWindowManager.Process();

			if (this.pageCount == 0)
			{
				GUIControl.ClearControl(this.GetID, facadeView.GetID);
				this.facadeView.View = this.DefaultView;

				GUIListItem item = new GUIListItem();
				item = new GUIListItem();
				item.Label = "..";

				//if (this.SearchString == null || this.SearchString.Length == 0)
				//{
				//    item.Path = "UKNova";
				//}
				//else
				//{
					item.Path = "..";
				//}

				item.IsFolder = true;
				item.IconImage = "defaultVideo.png";
				facadeView.Add(item);
			}

			dtUKNova.GetPage(this.pageCount, this.SearchString);

			// Keep counter in synch
			this.pageCount++;

			foreach (DataRow row in dtUKNova.Rows)
			{
				GUIListItem item = new GUIListItem();
				item.Label = row["name"].ToString();
				item.IsFolder = false;
				item.IconImage = "defaultVideo.png";
				item.Path = row["url"].ToString();

				//if (this.downloads != null)
				//{
				//    foreach (Download dl in downloads)
				//    {
				//        if (dl.TorrentInformation.Name == item.Label)
				//        {
				//            item.Label2 = dl.DownloadStats.Status + " (" + String.Format("{0:P2}", Double.Parse(dl.DownloadStats.Completed.ToString()) / 1000) + ")";
				//        }
				//    }
				//}


				facadeView.Add(item);
			}

			if (dtUKNova.Rows.Count == 0)
			{
				GUIListItem item = new GUIListItem();
				item.Label = "<The End>";
				item.IsFolder = true;
				// item.IconImage = "defaultVideo.png";
				// item.Path = row["url"].ToString();
				facadeView.Add(item);
			}

			this.RefreshTitle();
			GUIControl.FocusControl(this.GetID, this.facadeView.GetID);
		}


        //private void ShowDownloads()
        //{
        //    this.detailbox.Label = "";

        //    _AzureusTimer.Start();

        //    // Get Status from AZ
        //    this.downloads = azureusHost.GetDownloads();

        //    if (this.ListMode != "Torrents.Azureus"  || this.downloads.Length != this.facadeView.Count) 
        //    {
        //        this.ListMode = "Torrents.Azureus";

        //        this.selectedOutline = null;

        //        GUIControl.ClearControl(this.GetID, facadeView.GetID);
        //        this.facadeView.View = this.DefaultView;
        //    }

        //    GUIListItem item;

        //    int i = 0;

        //    foreach (Download dl in downloads)
        //    {
        //        if (i < facadeView.Count)
        //        {
        //            item = facadeView[i];
        //        }
        //        else
        //        {
        //            item = new GUIListItem();
        //        }

        //        item.Label = dl.TorrentInformation.Name;
        //        string DownLoadPercent = String.Format("{0:P2}", Double.Parse(dl.DownloadStats.Completed.ToString()) / 1000);
        //        string UploadPercent = String.Format("{0:P2}", ((double)dl.DownloadStats.Uploaded / dl.TorrentInformation.Size));
        //        item.Label2 = dl.DownloadStats.Status + " ( " + DownLoadPercent + " / " + UploadPercent + ") ";
        //        item.IsFolder = false;
        //        item.IconImage = "defaultVideo.png";
        //        item.Path = dl.TorrentInformation.GetHashCode().ToString();
        //        item.TVTag = dl;

        //        if (i >= facadeView.Count)
        //        {
        //            facadeView.Add(item);
        //        }

        //        i++;

        //        this.ShowStats();
        //    }			
        //}

		
		//private void RemoveDownLoad()
		//{
		//    GUIListItem item = this.facadeView.SelectedListItem;

		//    if (item != null)
		//    {
		//        Download dl = item.TVTag as Download;
		//        if (dl != null)
		//        {
		//            azureusHost.RemoveDownload(dl);
		//        }
		//    }
		//}

		//private void ToggleDownload()
		//{
		//    GUIListItem item = this.facadeView.SelectedListItem;

		//    if (item != null)
		//    {
		//        Download dl = item.TVTag as Download;
		//        if (dl != null)
		//        {
		//            if (dl.DownloadStats.Status == "Stopped")
		//            {
		//                azureusHost.StartDownload(dl);
		//            }
		//            else
		//            {
		//                azureusHost.StopDownload(dl);
		//            }
		//        }
		//    }
		//}


//        private void AzureusAction(string Action)
//        {
//            GUIListItem item = this.facadeView.SelectedListItem;

//            if (item != null)
//            {
//                Download dl = item.TVTag as Download;
//                if (dl != null)
//                {
//                    switch (Action)
//                    {
//                        case "Remove":

//                            GUIDialogYesNo dlgYesNo = (GUIDialogYesNo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_YES_NO);
//                            if (null == dlgYesNo) return;
//                            dlgYesNo.SetHeading(GUILocalizeStrings.Get(433));//delete watched recordings?
//                            dlgYesNo.SetLine(1, dl.TorrentInformation.Name);
////							dlgYesNo.SetLine(2, String.Empty);
////							dlgYesNo.SetLine(3, String.Empty);
//                            dlgYesNo.SetDefaultToYes(true);
//                            dlgYesNo.DoModal(GetID);

//                            if (dlgYesNo.IsConfirmed) 
//                            {
//                                azureusHost.RemoveDownload(dl);
//                            }

//                            break;
//                        case "Toggle":
//                            if (dl.DownloadStats.Status == "Stopped")
//                            {
//                                azureusHost.StartDownload(dl);
//                            }
//                            else
//                            {
//                                azureusHost.StopDownload(dl);
//                            }
//                            break;
//                        case "Start":
//                            azureusHost.StartDownload(dl);
//                            break;
//                        case "Stop":
//                            if (dl.DownloadStats.Status == "Stopped")
//                            {
//                                this.AzureusAction("Remove");
//                            }
//                            else
//                            {
//                                azureusHost.StopDownload(dl);
//                            }
//                            break;
//                        case "Up":
//                            azureusHost.MoveDownloadUp(dl);
//                            break;
//                        case "Down":
//                            azureusHost.MoveDownloadDown(dl);
//                            break;
//                    }

//                    this.ShowDownloads();
//                }
//            }
//        }


        //private void uTorentAction(string Action)
        //{
        //    GUIListItem item = this.facadeView.SelectedListItem;

        //    if (item != null)
        //    {
        //        string dl = item.TVTag as string;
        //        if (dl != null)
        //        {
        //            switch (Action)
        //            {
        //                case "Remove":

        //                    GUIDialogYesNo dlgYesNo = (GUIDialogYesNo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_YES_NO);
        //                    if (null == dlgYesNo) return;
        //                    dlgYesNo.SetHeading(GUILocalizeStrings.Get(433));//delete watched recordings?
        //                    dlgYesNo.SetLine(1, item.Label);
        //                    //							dlgYesNo.SetLine(2, String.Empty);
        //                    //							dlgYesNo.SetLine(3, String.Empty);
        //                    dlgYesNo.SetDefaultToYes(true);
        //                    dlgYesNo.DoModal(GetID);

        //                    if (dlgYesNo.IsConfirmed)
        //                    {
        //                        uTorrentData.RemoveDownload(dl);
        //                    }

        //                    break;
        //                case "Toggle":
        //                    //if (dl.DownloadStats.Status == "Stopped")
        //                    //{
        //                    //    uTorrentData.StartDownload(dl);
        //                    //}
        //                    //else
        //                    //{
        //                    //    uTorrentData.StopDownload(dl);
        //                    //}
        //                    break;
        //                case "Start":
        //                    uTorrentData.StartDownload(dl);
        //                    break;
        //                case "Stop":
        //                    //if (dl.DownloadStats.Status == "Stopped")
        //                    //{
        //                    //    this.uTorentAction("Remove");
        //                    //}
        //                    //else
        //                    //{
        //                        uTorrentData.StopDownload(dl);
        //                    //}
        //                    break;
        //                case "Up":
        //                    uTorrentData.MoveDownloadUp(dl);
        //                    break;
        //                case "Down":
        //                    uTorrentData.MoveDownloadDown(dl);
        //                    break;
        //            }

        //            this.ShowDownloads();
        //        }
        //    }
        //}


		//private void StartDownload()
		//{
		//    GUIListItem item = this.facadeView.SelectedListItem;

		//    if (item != null)
		//    {
		//        Download dl = item.TVTag as Download;
		//        if (dl != null)
		//        {
		//            azureusHost.StartDownload(dl);
		//        }
		//    }
		//}



		//private void StopDownload()
		//{
		//    GUIListItem item = this.facadeView.SelectedListItem;

		//    if (item != null)
		//    {
		//        Download dl = item.TVTag as Download;
		//        if (dl != null)
		//        {
		//            azureusHost.StopDownload(dl);
		//        }
		//    }			
		//}


        //protected override void OnStop()
        //{
        //    this.AzureusAction("Stop");
        //}

        //protected override void OnPlay()
        //{
        //    this.AzureusAction("Start");
        //}


        //protected override void OnShowContextMenu()
        //{
        //    GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);

        //    if (dlg == null)
        //        return;

        //    dlg.Reset();
        //    dlg.SetHeading(924);                // Menu
        //    dlg.AddLocalizedString(335);		// Start
        //    dlg.AddLocalizedString(1032);		// Stop
        //    dlg.Add("Start");
        //    dlg.Add("Stop");
        //    dlg.Add("Move Up");
        //    dlg.Add("Move Down");
        //    dlg.Add("Remove");

        //    dlg.DoModal(GetID);

        //    switch (dlg.SelectedLabel)
        //    {
        //        case 0:       // Start
        //            {
        //                this.AzureusAction("Start");
        //                break;
        //            }

        //        case 1:      // Stop
        //            {
        //                this.AzureusAction("Stop");
        //                break;
        //            }

        //        case 2:      // Move Up
        //            {
        //                this.AzureusAction("Up");
        //                break;
        //            }
        //        case 3:      // Move Down
        //            {
        //                this.AzureusAction("Down");
        //                break;
        //            }
        //        case 4:      // Remove
        //            {
        //                this.AzureusAction("Remove");
        //                break;
        //            }
        //    }
        //}

	}
}